package com.easemob.helpdesk.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.easemob.helpdesk.adapter.PreviewPicturePagerAdapter;
import com.easemob.helpdesk.widget.MultipointTouchViewPager;
import com.housekeeperapp.R;

import java.io.ByteArrayOutputStream;



/**
 * Created by xiaolong.wei on 2019/3/13.
 */

public class PreviewPictureActivity extends BaseActivityV2 {
    public static final String CURRENT_INDEX = "currentIndex";
    public static final String FILE_URIS = "fileUris";
    public static final String IS_URL = "isUrl";
    public static final int SUCCESS = 1;
    public static final int FAILED = 2;
    PreviewPicturePagerAdapter mAdapter;

    @Override
    protected void initFindView() {
        viewpager=findViewById(R.id.viewpager);
        loading=findViewById(R.id.loading);
        View rl_back=findViewById(R.id.rl_back);
        rl_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    MultipointTouchViewPager viewpager;
    ProgressBar loading;
    private int current_index;
    private String uris;
    private boolean isUrl;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case SUCCESS:
                    viewpager.setAdapter(mAdapter);
                    if (isUrl){
                        if (current_index == -1) {
                            current_index = 0;
                        }
                        viewpager.setCurrentItem(current_index);
                        viewpager.setOffscreenPageLimit(3);
                    }
                    viewpager.setVisibility(View.VISIBLE);
                    loading.setVisibility(View.GONE);
                    break;
                case FAILED:
                    Toast.makeText(PreviewPictureActivity.this,"获取图片失败",Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preview_picture);
        initFindView();
        current_index = getIntent().getIntExtra(CURRENT_INDEX, -1);
        uris = getIntent().getStringExtra(FILE_URIS);
        isUrl = getIntent().getBooleanExtra(IS_URL,true);

        new Thread(new Runnable() {
            @Override
            public void run() {
                loadData();
            }
        }).start();

    }

    private void loadData(){
        if (!TextUtils.isEmpty(uris)){
            if (isUrl) {
                String[] fileUris = uris.split(",");
                if (fileUris != null && fileUris.length != 0) {
                    mAdapter = new PreviewPicturePagerAdapter(fileUris, this);

                }
            }else {//二进制流
                byte[] bytes = base64ToByte(uris);
                if (bytes!=null){
                    mAdapter = new PreviewPicturePagerAdapter(this,bytes);

                }
            }
            mHandler.sendEmptyMessage(SUCCESS);
        }else {
            mHandler.sendEmptyMessage(FAILED);
        }
    }

    public byte[] base64ToByte(String base64Data){
        byte [] input = Base64.decode(base64Data.split(",")[1], Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(input, 0, input.length);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100,bos);
        byte[] bytes = bos.toByteArray();

        return bytes;
    }

/*
    @OnClick(R.id.rl_back)
    public void onViewClicked() {
        finish();
    }
*/

    @Override
    protected void onStop() {
        super.onStop();
        if (mHandler != null){
            mHandler.removeCallbacksAndMessages(null);
        }
        mHandler = null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
